package com.telnikoff.ts.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JSplitPane;

import com.telnikoff.gf.GfContributor;
import com.telnikoff.gf.GfGuest;
import com.telnikoff.gf.GfHost;
import com.telnikoff.gf.GfMenuFactory;
import com.telnikoff.log.Log;

public class RootComponent implements GfGuest <JSplitPane>
{
	private static final String NAME = "Type System";
	private JSplitPane split;
	
	public RootComponent()
	{
		split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	}

	public void visit(GfHost host) 
	{
		Container c = host.getContainer(this);
		if (c instanceof JComponent)
		{
			JComponent j = (JComponent)c;
			Component top = j.getTopLevelAncestor();
			if (top instanceof Window)
				((Window)top).addWindowListener(new WindowListener()
			{
				public void windowOpened(WindowEvent e)      {}
				public void windowClosing(WindowEvent e)     { Log.info(NAME, "Bye!"); }
				public void windowClosed(WindowEvent e)      {}
				public void windowIconified(WindowEvent e)   {}
				public void windowDeiconified(WindowEvent e) {}
				public void windowActivated(WindowEvent e)   {}
				public void windowDeactivated(WindowEvent e) {}
			});
		}
		c.add(split);
	}

	public String getName() 
	{
		return NAME;
	}

	@Override
	public List<GfContributor<JSplitPane>> getContributors() 
	{
		List<GfContributor<JSplitPane>> cs = new ArrayList<GfContributor<JSplitPane>>();
		cs.add(new LeftComponent());
		cs.add(new RightComponent());
		return cs;
	}

	@Override
	public JSplitPane getJComponent() 
	{
		return split;
	}

	@Override
	public void contribute(GfMenuFactory mf) 
	{
		mf.addItem("/New/Simple/Way", new AbstractAction("Some"){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}});
		
		mf.addItem("/New/Hard/Way", new AbstractAction("Another"){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}});

	}
}